<?php
class PostCategoryController extends  Controller{

	public function index(){
		$this->getSmarty();
		if(empty( $_SESSION["template"])){
			$_SESSION["template"]="default";
		}
		$this->smarty->display("admin/postCategory/postCategory.tpl");
	}
	
	/**
	 * 获取树
	 * Enter description here ...
	 */
	public function getSubMenu(){
		require_once MODEL . DS . 'Tree.php';
		$dbutil = $this->getDB();
		$categoryId = $_POST ['category_id'];
		
		$sql = "select * from post_category ";
		$rss = $dbutil->get_results($sql);
		$result;
		foreach ($rss as $rs){
			$treeNode = $rs->parent;
			$tree = new Tree();
			if($treeNode==0){
				if(!isset($categoryId)||$categoryId=="1"){
					$tree->checked=true;
				}
				$tree->id=$rs->category_id;
				$tree->pId=$rs->parent;
				$tree->name=$rs->category_name;
				$tree->open=true;
				$tree->isParent=true;
				$tree->desc=$rs->description;
				$tree->cIndex=$rs->c_index;
				$result[] = array (
				$tree
				);
			}else{
				$count = $this->countChildNode($treeNode);
				if($count>0){
					$tree->isParent=false;
				}
				if($categoryId == $rs->category_id){
					$tree->checked=true;
				}
				$tree->open=true;
				$tree->id=$rs->category_id;
				$tree->pId=$rs->parent;
				$tree->name=$rs->category_name;
				$tree->desc=$rs->description;
				$tree->cIndex=$rs->c_index;
				
				$result[] = array (
				$tree
				);
			}
			$isParent = false;
		}
		echo json_encode ($result );

	}
	
	public function countChildNode($treeNode){
		require_once SERVICE . DS . 'admin/PostCategoryService.class.php';	
		$postCategoryService = new PostCategoryService( $this->getDB () );

		$count = $postCategoryService->countChildNode ($treeNode);
		return $count;
	}
	/**
	 * 删除节点
	 * Enter description here ...
	 */
	public function removePostCategory(){
		$this->getSmarty();
		require_once SERVICE . DS . 'admin/PostCategoryService.class.php';
		$id =  $_POST ['category_id'];
		$postCategoryService = new PostCategoryService( $this->getDB () );

		$status = $postCategoryService->removePostCategory ($id);
		if($status=='1'){
			echo json_encode("OK");
		}else {
			echo json_encode("ERROR");
		}
	}
	/**
	 * 增加节点
	 * Enter description here ...
	 */
	public function addPostCategory(){
		$this->getSmarty();
		require_once SERVICE . DS . 'admin/PostCategoryService.class.php';

		$postCategoryService = new PostCategoryService( $this->getDB () );

		//获取所有参数
		$argsArr = $_POST;
		$data = array(
				"category_name"=>$argsArr['category_name'],
				"description"=>$argsArr['description'],
				"c_index"=>$argsArr['c_index'],
				"parent"=>$argsArr['category_id']
		);
		$status = $postCategoryService->addPostCategory ($data);
		if($status=='1'){
			echo json_encode("OK");
		}else {
			echo json_encode("ERROR");
		}
	}
	/**
	 * 更新节点
	 * Enter description here ...
	 */
	public function updatePostCategory(){
		$this->getSmarty();
		require_once SERVICE . DS . 'admin/PostCategoryService.class.php';

		$postCategoryService = new PostCategoryService( $this->getDB () );

		//获取所有参数
		$argsArr = $_POST;
		$data = array(
				"category_name"=>$argsArr['category_name'],
				"description"=>$argsArr['description'],
				"c_index"=>$argsArr['c_index'],
				"parent"=>$argsArr['parent']
		);
		$where = array("category_id"=>$argsArr['category_id']);
		$status = $postCategoryService->updatePostCategory ($data, $where);
		if($status=='1'){
			echo json_encode("OK");
		}else {
			echo json_encode("ERROR");
		}
	}
	/**获取所有分类名称*/
	function getCategoryName(){
		$this->getSmarty();
		require_once SERVICE . DS . 'admin/PostCategoryService.class.php';

		$postCategoryService = new PostCategoryService( $this->getDB () );

		$postsCategory = $postCategoryService->getCategoryName ($conditions);
		
		echo json_encode ( $postsCategory );
	}
	
}